<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 商品管理控制器
 */
class Product extends AdminBase
{

    /**
     * 商品列表
     */
    public function productList()
    {
        $where = $this->logicProduct->getWhere($this->param);

        $list = $this->logicProduct->getProductList($where);
        
        $this->assign('list', $list);
        
        return $this->fetch('product_list');
    }
    
    /**
     * 商品添加
     */
    public function productAdd()
    {
        IS_POST && $this->jump($this->logicProduct->productAdd($this->param));

        $this->productAddEditCommon();
        
        // 为添加操作设置空的info数组，确保模板中的默认值正确显示
        $this->assign('info', []);
        
        return $this->fetch('product_edit');
    }
    
    /**
     * 商品编辑
     */
    public function productEdit()
    {
        IS_POST && $this->jump($this->logicProduct->productEdit($this->param));

        $this->productAddEditCommon();
        
        $info = $this->logicProduct->getProductInfo(['id' => $this->param['id']]);
        
        !empty($info) && $info['img_ids_array'] = str2arr($info['img_ids']);
        
        $this->assign('info', $info);
        
        return $this->fetch('product_edit');
    }

    /**
     * 添加/编辑公共函数
     */
    protected function productAddEditCommon()
    {
        $this->assign('categories', $this->logicProduct->productAddEditCommon());
    }
    
    /**
     * 商品删除
     */
    public function productDelete()
    {
        $this->jump($this->logicProduct->productDel($this->param));
    }
    
    /**
     * 商品详情
     */
    public function productDetail()
    {
        $info = $this->logicProduct->getProductDetail(['id' => $this->param['id']]);
        
        $this->assign('info', $info);
        
        return $this->fetch('product_detail');
    }
    
    /**
     * 商品状态切换
     */
    public function productStatus()
    {
        $this->jump($this->logicProduct->productStatus($this->param));
    }
    
    /**
     * 商品导出
     */
    public function productExport()
    {
        $where = $this->logicProduct->getWhere($this->param);
        
        $this->logicProduct->exportProductList($where);
    }
    
    /**
     * 商品导入页面
     */
    public function productImport()
    {
        IS_POST && $this->jump($this->logicProduct->productImport($this->param));
        
        // 获取导入历史记录
        $importLogs = $this->logicProduct->getImportLogs();
        
        $this->assign('importLogs', $importLogs);
        
        return $this->fetch('product_import');
    }
    
    /**
     * 下载导入模板
     */
    public function downloadTemplate()
    {
        $this->logicProduct->downloadImportTemplate();
    }
    
    /**
     * 获取导入进度（AJAX）
     */
    public function getImportProgress()
    {
        $result = $this->logicProduct->getImportProgress($this->param);
        return json($result);
    }
    
    /**
     * 获取导入详情（AJAX）
     */
    public function getImportDetail()
    {
        $result = $this->logicProduct->getImportDetail($this->param);
        return json($result);
    }
    
    /**
     * 商品分类管理
     */
    public function category()
    {
        IS_POST && $this->jump($this->logicProduct->categoryManage($this->param));
        
        $this->assign('list', $this->logicProduct->getCategoryList());
        
        return $this->fetch('product_category');
    }
    
    /**
     * 获取分类选项（AJAX）
     */
    public function getCategoryOptions()
    {
        $result = $this->logicCategory->getCategoryOptions();
        return json($result);
    }
    

    
    /**
     * 商品库存管理
     */
    public function stock()
    {
        IS_POST && $this->jump($this->logicProduct->stockManage($this->param));
        
        $info = $this->logicProduct->getProductDetail(['id' => $this->param['id']]);
        
        $this->assign('info', $info);
        
        return $this->fetch('product_stock');
    }
    
    /**
     * 数据状态设置
     */
    public function setStatus()
    {
        $this->jump($this->logicAdminBase->setStatus('Product', $this->param));
    }
    
    /**
     * 排序
     */
    public function setSort()
    {
        $this->jump($this->logicAdminBase->setSort('Product', $this->param));
    }
    
    /**
     * 获取价格信息
     * @deprecated 已废弃，请使用 Price::getPriceInfo() 代替
     */
    public function getPriceInfo()
    {
        $result = $this->logicPrice->getPriceInfo($this->param);
        return json($result);
    }
    
    /**
     * 获取价格历史
     * @deprecated 已废弃，请使用 Price::history() 代替
     */
    public function getPriceHistory()
    {
        $result = $this->logicPrice->getPriceHistory($this->param);
        return json($result);
    }
    
    /**
     * 获取价格趋势
     * @deprecated 已废弃，请使用 Price::trend() 代替
     */
    public function getPriceTrend()
    {
        $result = $this->logicPrice->getPriceTrend($this->param);
        return json($result);
    }
    
    /**
     * 保存价格变动
     * @deprecated 已废弃，请使用 Price::change() 代替
     */
    public function savePriceChange()
    {
        $result = $this->logicPrice->savePriceChange($this->param);
        return json($result);
    }
}